<template>
  <van-pull-refresh v-model="refreshing" :success-text="successText" @refresh="onRefresh">
    <van-list ref="vanList" v-model="loading" :offset="300" :immediate-check="immediateCheck" :error.sync="error" :finished="finished" :loading-text="loadingText" :error-text="errorText" :finished-text="finishedText" @load="onLoad">
      <Slide @left="leftHandle" @right="rightHandle">
        <slot name="list">
          <div class="help-floor-list van-clearfix">
            <slot />
          </div>
        </slot>
      </Slide>
    </van-list>
  </van-pull-refresh>
</template>
<script>
import Slide from '@/components/Slide'
export default {
  name: 'HelpList',
  components: {
    Slide
  },
  props: {
    load: {
      type: Function,
      default: undefined
    },
    refresh: {
      type: Function,
      default: undefined
    },
    finished: {
      type: Boolean,
      default: false
    },
    setPageSize: {
      type: Number,
      default: 20
    },
    setPage: {
      type: Number,
      default: 1
    },
    resetFun: {
      type: Function,
      default: () => {}
    },
    componentName: {
      type: String,
      default: ''
    },
    immediateCheck: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      refreshing: false,
      loading: false,
      error: false,
      loadingText: '加载中...',
      errorText: '加载错误！单击重试！',
      finishedText: '没有更多了',
      successText: '刷新成功',
      page: 1,
      pageSize: 10,
      isActive: true
    }
  },
  created() {
    this.resetPageData()
  },
  activated() {
    this.isActive = true
  },
  deactivated() {
    this.isActive = false
  },
  methods: {
    async promiseFun(fun, eventName, ...args) {
      if (fun instanceof Function) {
        return await fun(...args)
      }
      this.$emit(eventName, ...args)
    },
    onLoad() {
      this.promiseFun(this.load, 'loaded', this.page, this.pageSize).then(res => {
        this.error = false
        this.page++
        return res
      }).catch(e => {
        if (e === 'noError') {
          this.errorText = ''
        } else {
          this.errorText = '加载错误！单击重试！'
        }
        this.$emit('update:finished', false)
        this.error = true
        return Promise.reject(e)
      }).finally(() => {
        this.loading = false
        this.refreshing = false
      })
    },
    onRefresh() {
      this.resetPageData()
    },
    // 重置分页数据
    resetPageData(page, pageSize) {
      if (!this.isActive) return
      this.pageSize = pageSize || this.setPageSize
      this.page = page || this.setPage
      this.$emit('update:finished', false)
      this.error = false
      this.loading = false
      this.resetFun()
      if (this.$refs.vanList) {
        this.$refs.vanList.check()
      }
    },
    leftHandle() {
      this.dispatch(this.componentName, 'helpPrev')
    },
    rightHandle() {
      this.dispatch(this.componentName, 'helpNext')
    }
  }
}
</script>

<style lang="less">
.help-floor-list{
  padding: 0 10px;
}
</style>
